package com.litan.chapterfive;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ClearableEditText extends LinearLayout {

    private EditText editText;
    private Button clearButton;

    public ClearableEditText(Context context){
        this(context, null);
    }

    public ClearableEditText(Context context, AttributeSet attrs){
        this(context, attrs, 0);
    }

    public ClearableEditText(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);

        //用xml膨胀布局 也可用代码构建布局-->详见P109
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(infService);
        li.inflate(R.layout.my_compound_view, this, true);

        // 获取子控件的引用
        editText = (EditText) findViewById(R.id.editText);
        clearButton = (Button) findViewById(R.id.clearButton);

        // 用于连接功能
        hookupButton();
    }

    private void hookupButton(){
        clearButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                editText.setText("");
            }
        });
    }
}
